'use strict';
module.exports = {
	/**
	 * 删除文章
	 * @url client/article/kh/delete 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} _id  ID
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let { _id } = data;
		
		let pattern = /<img.+?src="(.+?)".*?>/g;
		
		let dbName = "db-article";

		let dbRes = await vk.baseDao.findById({
			dbName,
			id: _id
		})
		
		if(!vk.pubfn.isNull(dbRes)) {
			res["data"] = await vk.baseDao.del({
				dbName,
				whereJson: {
					_id: _id,
					uid: userInfo._id
				}
			})
			
			let match, fileList = [dbRes.blogs_cover];
			while (match = pattern.exec(dbRes.content)) {
				let url = match[1];
				fileList.push(url)
			}
			
			await vk.callFunction({
				url: 'admin/upload/pub/delete',
				data: {
					image: fileList
				}
			})
			
			if(res.data) {
				delete res["data"];
				res["msg"] = "删除成功！";
			} else {
				res["code"] = -1;
				res["msg"] = "删除失败或不存在！";
			}
		} else {
			res["code"] = -1;
			res["msg"] = "删除失败或不存在！";
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
